//
//  ViewController.swift
//  SwiftDemo
//
//  Created by xinchen on 2023/7/7.
//

import UIKit

class CLModel : NSObject {
    var name: String = ""
    
    init(name: String) {
        self.name = name
    }
}

class ViewController: UIViewController {
    
    
    static var delegateDict: [String: String] = {
        ViewController.initPaymentPlatform()
    }()
    
    static func initPaymentPlatform() -> [String: String] { // 新增支付平台需要在这里进行维护
        var dict: [String: String] = [:]
        dict["AB"] = "CD"
        dict["ABC"] = "D"
        return dict
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.white
        
        self.removeDuplicates()
        
        
                
        let array = ["11", "22", "11", "22", "3", "11", "44", "5", "1", "4", "5", "1", "4"];
        let temp: [CLModel] = array.map { value -> CLModel? in
            if value.count > 1 {
                return CLModel(name: value)
            }
            return nil
        }.compactMap { $0 }
        
        
//        let button = UIButton.init(frame: CGRect(x: 50, y: 120, width: 120, height: 50))
//        button.setTitle("click", for: .normal)
//        button.backgroundColor = .brown
//        button.addTarget(self, action: #selector(clickAction), for: .touchUpInside)
//        self.view.addSubview(button)
        
        // 创建父视图
                let parentView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
                view.addSubview(parentView)
                
                // 创建 AView
                let aView = UIView(frame: parentView.bounds)
                aView.backgroundColor = .clear
                parentView.addSubview(aView)
                
                // 在 AView 中添加按钮
                let aButton = UIButton(type: .system)
                aButton.frame = CGRect(x: 50, y: 50, width: 100, height: 50)
                aButton.setTitle("Button in AView", for: .normal)
                aButton.addTarget(self, action: #selector(aButtonTapped), for: .touchUpInside)
                aView.addSubview(aButton)
                
                // 创建 BView
                let bView = UIView(frame: parentView.bounds)
                bView.backgroundColor = .blue
                parentView.addSubview(bView)
                
                // 在 BView 中添加按钮
                let bButton = UIButton(type: .system)
                bButton.frame = CGRect(x: 50, y: 120, width: 100, height: 50)
                bButton.setTitle("Button in BView", for: .normal)
                bButton.addTarget(self, action: #selector(bButtonTapped), for: .touchUpInside)
                bView.addSubview(bButton)
        
        GPAlgorithm.shared.minWindow("ADOBECODEBANC", "ABC")
        var arr = [1,2,3,0,0,0]
        GPAlgorithm.shared.merge(&arr, 3, [2,5,6], 3)
        
        let result = GPAlgorithm.shared.mergeList([-1, 11, 12, 13, 14, 15], [-2, 0, 2, 9, 10])
        print("\(result)")
        
    }
    
    @objc func aButtonTapped() {
            print("Button in AView tapped")
        }
        
        @objc func bButtonTapped() {
            
            print("Button in BView tapped, \(ViewController.delegateDict)")
        }
    
    @objc func clickAction() {
        let vc: FirstVC = FirstVC.init()
        self.present(vc, animated: true)
    }

    func removeDuplicates() -> Int {
        maxProfit()
        let nums: Array<Int> = [-1]
        
        let array = nums.suffix(2) + nums.prefix(2)
        
//        let middle: Int = nums.count % 2 > 0 ? nums.count / 2 + 1 : nums.count / 2
//        var dict: Dictionary<Int, Int> = [:]
//        
//        for value in nums {
//            var count: Int = dict[value] ?? 0
//            count = count + 1
//            dict[value] = count
//            if count >= middle {
//                return count
//            }
//        }
//        nums.suffix(3)
        return 0
    }
    
    func maxProfit() -> Int {
        cutOutArray()
        let prices: Array<Int> = [7,1,3,6,4]
        var result = 0
        
        var buy: Int = prices.first ?? 0
        var sell = 0
        
        for value in prices {
            if value < buy {
                buy = value
            } else {
                result = value - buy
                if result > 0 {
                    sell = sell + result
                    buy = value
                }
            }
        }
        
        return sell
    }
    
    func cutOutArray() -> Int {
        hIndex()
        let nums: Array<Int> = [7,14,6,5,4,3,1,4/*7*/,9,0,1,2,9,0,3]//14
        let count = nums.count
        var end = 0
        var maxPosition = 0
        var steps = 0
        
        for index in 0..<count-1 {
            if maxPosition < index + nums[index] {
                maxPosition = index + nums[index]
            }
            if (index == end) {
                end = maxPosition
                steps = steps + 1
            }

        }
        return steps
    }

    func hIndex() -> Int {
        productExceptSelf()
        var nums: Array<Int> = [3,0,6,1,5,4,7]
        
        let _ = nums.reversed()
                
        var result = 0
        let sc = Array(nums.sorted().reversed())
        for i in 0..<nums.count {
            if sc[i] > i {
                result = i + 1
            }
        }
        return result
    }
    
    func productExceptSelf() -> [Int] {
        canCompleteCircuit()
        let nums: Array<Int> = [3,0,6,1,5,4,7]
        var resArr = Array(repeating: 0, count: nums.count)
        
        var temp = 1
        for i in 0 ..< nums.count {
            resArr[i] = temp
            temp *= nums[i]
        }
        temp = 1
        for i in (0 ..< nums.count).reversed() {
            resArr[i] *= temp
            temp *= nums[i]
        }
        
        return resArr
    }
    /// 134. 加油站
    func canCompleteCircuit() -> Int {
        let gas = [5,1,2,3,4] // 加油量
        let cos = [4,4,1,5,1] // 耗油量
        let count = gas.count
        
        var result = -1
        var temp = 0
        var resArr = Array(repeating: 0, count: count)
        for index in 0..<count {
            var oli = temp + gas[index] // 加油
            oli = oli - cos[index] // 油耗
            resArr[index] = oli - temp
            temp = oli
            
        }
        
        if temp < 0 {
            return -1
        }        
        var tempValue = 0
        for index in 0..<resArr.count {
            tempValue = tempValue + resArr[index]
            
            if tempValue >= 0 {
                if resArr[index] >= 0 && result == -1 {
                    result = index
                }
            } else {
                tempValue = 0
                result = -1
            }
        }
        return result
    }
    

    
    /**
     n 个孩子站成一排。给你一个整数数组 ratings 表示每个孩子的评分。

     你需要按照以下要求，给这些孩子分发糖果：

     每个孩子至少分配到 1 个糖果。
     相邻两个孩子评分更高的孩子会获得更多的糖果。
     请你给每个孩子分发糖果，计算并返回需要准备的 最少糖果数目 。
     */
    class Solution {
        func candy(_ ratings: [Int]) -> Int {
            let length = ratings.count
            if length == 1 {
                return 1
            }
            var leftArray = Array.SubSequence(repeating: 1, count: length)
            for index in 1..<length {
                if ratings[index] > ratings[index-1] {
                    leftArray[index] = leftArray[index-1] + 1
                }
            }
            
            var rightArray = Array.SubSequence(repeating: 1, count: length)
            for index in 0..<length-1 {
                if ratings[length-2-index] > ratings[length-1-index] {
                    rightArray[length-2-index] = rightArray[length-1-index] + 1
                }
            }
            
            var count = 0
            for index in 0..<length {
                count = count + max(leftArray[index], rightArray[index])
            }
            
            return count
        }
    }
    
}


